package org.luxor.commons.core.utils.verify;


import org.apache.commons.lang.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;

/**
 * 通用校验工具类
 *
 * @author HabenChan @date 2018/5/31.
 */
@SuppressWarnings("All")
public class GeneralCheckUtils {

    /**
     * ============================================通讯录模块==============================================================
     */

    //部门名称校验规则：3-32位，中文，数字，字母，且第一位只能是中文和字母
    private final static String DEPT_PATTERN = "^[\\u4E00-\\u9FA5A-Za-z]{1}[\\u4E00-\\u9FA5A-Za-z0-9]{2,31}$";
    //标签名称校验规则：1-32位，中文，数字，字母
    private final static String TAG_PATTERN = "^[\\u4E00-\\u9FA5A-Za-z0-9]{1,31}$";
    //扩展字段名称校验规则：1-32位，中文，数字，字母
    private final static String EXT_PATTERN = "^[\\u4E00-\\u9FA5A-Za-z0-9]{1,32}$";
    //人员帐号：不超过64个字，支持字母、数字、点(.)、减号(-)、下划线(_)
    private final static String ACCOUNT_PATTERN = "[\\w\\d\\.\\-\\_]{1,64}";
    // 人员名称
    private final static String PERSON_NAME_PATTERN = "[^\"“”'‘'’]{1,32}";
    //人员英文名称 1-64字符，字母
    private final static String PERSON_ENNAME_PATTERN = "^[A-Za-z]{1,64}";
    //身份证号（简单版）
    private final static String PERSON_IDCARD_PATTERN = "^((\\d{18})|([0-9x]{18})|([0-9X]{18}))$";
    private final static String PERSON_PHONE_NUMBER = "((\\+|\\-)|\\d){0,32}";
    private final static String PERSON_TEL_NUMBER = "(\\-|\\d){0,32}";
    private final static String PHONE_PATTERN = "[\\d]{11}";
    private final static String PHONE_PATTERN2 = "[\\d]{1,11}";
    private final static String AREA_PATTERN = "[\\d]{0,8}";
    //手机号32个字符以内数字，+，-
    private final static String PERSON_PHONE_PATTERN = "^[\\d\\+\\-]{1,32}$";
    //座机号32个字符以内数字，-
    private final static String PERSON_TELEPHONE_PATTERN = "^[\\d\\-]{1,32}$";
    //  private final static String EMAIL_PATTERN = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+){1,64}$";
//    private final static String EMAIL_PATTERN = "^[\\w.\\-]+@(?:[a-z0-9]+(?:-[a-z0-9]+)*\\.)+[a-z]{2,3}$";
    private final static String EMAIL_PATTERN = "^[A-Za-z0-9_.]+@(?:[a-z0-9]+(?:-[a-z0-9]+)*\\.)+[a-z]{2,3}$";
    private final static String CARD_VALUE = "[^\"“”'‘'’]{1,32}";
    private final static String CARDID_VALUE = "^((\\d{18})|([0-9x]{18})|([0-9X]{18}))$";
    private final static String DEPT_ID = "[\\d]{1,50}";
    private final static String DEPT_IDS = "[\\d;]{1,400}";
    private final static String TOPS = "[\"是\"\"否\"\";\"]{1,400}";
    private final static String DATE = "\\d{4}(\\-|\\/)\\d{1,2}(\\-|\\/)\\d{1,2}";
    private final static String ALL_DUTIES = "[;,]+";
    //英文名
    private final static String ENGNAME = "[a-zA-Z]{0,64}";
    //显示名
    private final static String DISNAME = "[^\"“”'‘'’]{1,32}";
    //是否是数字
    private final static String ISNUMBER = "[0-9]*";
    /**
     * ============================================通讯录模块==============================================================
     */




    /*===============================================开发商注册============================================================*/

    // 开发商账号
    private final static String DEV_ACCOUNT_PATTERN = "[\\w\\d\\_]{1,32}";
    //企业名称
    private final static String DEV_NAME = "^[\\u4E00-\\u9FA5A-Za-z0-9\\.]{1,64}$";
    //企业简称
    private final static String DEV_SHORT_NAME = "^[\\u4E00-\\u9FA5A-Za-z0-9\\.]{1,64}$";
    //姓名
    private final static String DEV_FIRSTNAME = "^[\\u4E00-\\u9FA5A-Za-z]{1,32}$";
    //邮箱
    private final static String DEV_EMAIL = "^[\\w.\\-]+@(?:[a-z0-9]+(?:-[a-z0-9]+)*\\.)+[a-z]{2,3}$";
    //手机号
    private final static String DEV_MOBILE = "^[-+0-9]{1,16}$";
    //企业地址
    private final static String DEV_ADDRESS = ".{1,128}";
    //法人代表
    private final static String DEV_CORPORAION = "^[\\u4E00-\\u9FA5A-Za-z]{1,64}$";
    //企业电话
    private final static String DEV_PHONE = "^[-0-9]{1,16}$";
    //上级管理员
    private final static String DEV_SUPER_ACCOUNT = "[a-zA-z0-9_\\-\\.]{1,64}";
    //密码
    private final static String DEV_PASSWORD = "^(?![0-9]+$)(?![a-zA-Z]+$)(?![!@#$%^&*()]+$)(?![0-9!@#$%^&*()]+$)(?![a-zA-z!@#$%^&*()]+$)[0-9A-Za-z!@#$%^&*()]{8,16}$";

    /*====================================================================================================================*/


    /**
     * 8-16个字符，字母，数字，大小写，特殊符号，且至少包含3种类型
     */
    private final static String DEV_ACCOUNT_PWD_PATTERN = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,16}$";


    public static boolean checkDevAccount(String account) {
        if (StringUtils.isBlank(account)) {
            return false;
        }
        return Pattern.matches(DEV_ACCOUNT_PATTERN, account);
    }

    /**
     * 判断开发商密码是否符合规则
     *
     * @param pwd
     * @return
     */
    public static boolean checkDevAccountPWD(String pwd) {
        if (StringUtils.isBlank(pwd)) {
            return false;
        }
        return Pattern.matches(DEV_ACCOUNT_PWD_PATTERN, pwd);
    }

    /**
     * 检查部门名称是否符合规则
     *
     * @param name
     * @return
     */
    public static boolean checkDeptName(String name) {
        if (StringUtils.isEmpty(name)) {
            return false;
        }
        return Pattern.matches(DEPT_PATTERN, name);
    }

    public static boolean checkCard(String card) {
        if (StringUtils.isEmpty(card)) {
            return false;
        }
        return Pattern.matches(CARD_VALUE, card);
    }

    public static boolean checkCardValue(String cardValue) {
        if (StringUtils.isEmpty(cardValue)) {
            return false;
        }
        return Pattern.matches(CARDID_VALUE, cardValue);
    }


    public static boolean checkDeptId(String id) {
        if (StringUtils.isEmpty(id)) {
            return false;
        }
        return Pattern.matches(DEPT_ID, id);
    }

    public static boolean checkDeptIds(String id) {
        if (id.replaceAll(" ", "").replaceAll("[;；]", "").length() == 0) {
            return false;
        }
        return Pattern.matches(DEPT_IDS, id);
    }

    public static boolean checkTop(String top) {
        if (StringUtils.isEmpty(top)) {
            return true;
        }
        return Pattern.matches(TOPS, top);
    }

    public static boolean checkAllDuties(String allDuties) {

        return Pattern.matches(ALL_DUTIES, allDuties);
    }

    public static boolean checkDate(String date) {

        return Pattern.matches(DATE, date);
    }


    //检查手机号码是否合法
    public static boolean checkPhoneNumber(String phone) {
        return Pattern.matches(PERSON_PHONE_NUMBER, phone);
    }

    //检查座机号是否合法
    public static boolean checkTelNumber(String phone) {
        return Pattern.matches(PERSON_TEL_NUMBER, phone);
    }

    /**
     * 检查标签名称是否符合规则
     *
     * @param name
     * @return
     */
    public static boolean checkTagName(String name) {
        return Pattern.matches(TAG_PATTERN, name);
    }

    /**
     * 检查人员帐号是否符合规则
     *
     * @param account
     * @return
     */
    public static boolean checkAccount(String account) {
        if (StringUtils.isEmpty(account)) {
            return false;
        }
        return Pattern.matches(ACCOUNT_PATTERN, account);
    }

    public static boolean checkAccountRe(String account) {

        return Pattern.matches(ACCOUNT_PATTERN, account);
    }


    public static boolean checkEngName(String engName) {

        return Pattern.matches(ENGNAME, engName);
    }

    public static boolean checkDisName(String disname) {
        if (StringUtils.isEmpty(disname)) {
            return false;
        }
        return Pattern.matches(DISNAME, disname);
    }

    /**
     * 名称
     */
    public static boolean checkPersonName(String name) {
        return Pattern.matches(PERSON_NAME_PATTERN, name);
    }

    /**
     * 英文名称
     */
    public static boolean checkPersonENName(String name) {
        return Pattern.matches(PERSON_ENNAME_PATTERN, name);
    }

    /**
     * 手机号码
     */
    public static boolean checkPersonPhone(String phone) {
        return Pattern.matches(PERSON_PHONE_PATTERN, phone);
    }

    /**
     * 座机号码
     */
    public static boolean checkPersonTelePhone(String telephone) {
        return Pattern.matches(PERSON_TELEPHONE_PATTERN, telephone);
    }


    /**
     * @param idCard
     * @return
     */
    public static boolean checkIDCard(String idCard) {
        if (StringUtils.isBlank(idCard)) {
            return false;
        }
        return Pattern.matches(PERSON_IDCARD_PATTERN, idCard);
    }

    /**
     * 生日校验
     */
    public static boolean checkDateFormat(String date, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        try {
            LocalDate.parse(date, formatter);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    /**
     * 手机号码
     */
    public static final boolean checkPhone(String phone) {
        return Pattern.matches(PHONE_PATTERN, phone);
    }

    public static final boolean checkPhone2(String phone) {
        return Pattern.matches(PHONE_PATTERN2, phone);
    }


    public static final boolean checkAreaCode(String phone) {
        return Pattern.matches(AREA_PATTERN, phone);
    }

    /**
     * email
     */
    public static final boolean checkMail(String email) {
        if (StringUtils.isEmpty(email)) {
            return true;
        }
        return Pattern.matches(EMAIL_PATTERN, email);
    }


    /**
     * 验证扩展字段名
     *
     * @param name
     * @return
     */
    public static boolean checkExtName(String name) {
        if (StringUtils.isEmpty(name)) {
            return false;
        }
        return Pattern.matches(EXT_PATTERN, name);
    }

    /**
     * 利用正则表达式判断字符串是否是数字
     *
     * @param value
     * @return
     */
    public static boolean isNumeric(String value) {
        if (StringUtils.isEmpty(value)) {
            return true;
        }
        return Pattern.matches(ISNUMBER, value);
    }


    public static boolean checkDevName(String devName) {
        if (StringUtils.isEmpty(devName)) {
            return false;
        }
        return Pattern.matches(DEV_NAME, devName);
    }

    public static boolean checkDevShortName(String devShortName) {
        if (StringUtils.isEmpty(devShortName)) {
            return false;
        }
        return Pattern.matches(DEV_SHORT_NAME, devShortName);
    }

    public static boolean checkDevFirstName(String devFirstName) {
        if (StringUtils.isEmpty(devFirstName)) {
            return false;
        }
        return Pattern.matches(DEV_FIRSTNAME, devFirstName);
    }

    public static boolean checkDevEmail(String devEmail) {
        if (StringUtils.isEmpty(devEmail)) {
            return false;
        }
        return Pattern.matches(DEV_EMAIL, devEmail);
    }

    public static boolean checkDevMobile(String devMobile) {
        if (StringUtils.isEmpty(devMobile)) {
            return false;
        }
        return Pattern.matches(DEV_MOBILE, devMobile);
    }

    public static boolean checkDevAddress(String devAddress) {
        if (StringUtils.isEmpty(devAddress)) {
            return false;
        }
        return Pattern.matches(DEV_ADDRESS, devAddress);
    }

    public static boolean checkDevCorporaion(String devCorporaion) {
        if (StringUtils.isEmpty(devCorporaion)) {
            return false;
        }
        return Pattern.matches(DEV_CORPORAION, devCorporaion);
    }

    public static boolean checkDevPhone(String devPhone) {
        if (StringUtils.isEmpty(devPhone)) {
            return false;
        }
        return Pattern.matches(DEV_PHONE, devPhone);
    }

    public static boolean checkDevSuperAccount(String superAccount) {
        if (StringUtils.isEmpty(superAccount)) {
            return false;
        }
        return Pattern.matches(DEV_SUPER_ACCOUNT, superAccount);
    }

    public static boolean checkDevPassword(String password) {
        if (StringUtils.isEmpty(password)) {
            return false;
        }
        return Pattern.matches(DEV_PASSWORD, password);
    }

    /**
     * 去除字符串首尾空格(包括半角，全角的空格)
     *
     * @param source 原字符串
     * @return 去除空格的字符串
     */
    public static String trimSpace(String source) {
        return source == null ? source : source.replaceAll("^[\\s　]*|[\\s　]*$", "");
    }
}
